package main

import (
	"context"
	"encoding/json"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"os"
)

var mongoClient *mongo.Client

type ShopInfo struct {
	Id   int64  `json:"id"`
	Name string `json:"name"`
	Body string `json:"body"`
}

func init() {
	clientOptions := options.Client().ApplyURI("mongodb://root:root@139.198.168.66:27017")
	client, err := mongo.Connect(context.Background(), clientOptions)
	if err != nil {
		fmt.Println(err)
		os.Exit(-1)
	}
	mongoClient = client
}

func main() {

	result := make([]ShopInfo, 0)
	collection := mongoClient.Database("dev").Collection("shop_info")
	filter := bson.D{}
	cursor, err := collection.Find(context.Background(), filter)
	if err != nil {
		fmt.Println(err)
		return
	}
	for cursor.Next(context.TODO()) {
		var temp ShopInfo
		err := cursor.Decode(&temp)
		if err != nil {
			continue
		}
		result = append(result, temp)
	}

	_ = cursor.Close(context.TODO())
	marshal, _ := json.Marshal(result)
	fmt.Println(string(marshal))
}
